<?php
load_ctl("admin_Common");
class Admin_PhotoController extends Admin_CommonController {
	function init(){
		parent::init();
		load_model('Photo');
		$this->photo = new Photo();
	}
	public function indexAction(){
		if(isset($_GET["id"]) && is_numeric($_GET["id"])){
			$id = intval($_GET["id"]);
		}else{
			$this->_redirect('/');
		}
		$page = isset($_GET["p"]) ? intval($_GET["p"]) : 1;
	    $page < 1 && $page = 1;
		$page_size = 15;
		$this->view->title = "相片信息";
		$this->view->photo_list = $this->photo->get_phoclass_info($id);
		
		$paginator = Zend_Paginator::factory($this->view->photo_list); 
		$paginator->setCurrentPageNumber($page);
		$paginator->setItemCountPerPage($page_size); 
		$this->view->paginator = $paginator;
		$this->view->mainphp = 'views/scripts/admin/index/photo.phtml';
		echo $this->view->render("index.phtml");
	}
	
	public function addphoAction(){
		if(isset($_GET["id"]) && is_numeric($_GET["id"])){
			$id = intval($_GET["id"]);
		}else{
			$this->_redirect('/');
		}
		$this->view->title = "添加相片";
		$this->view->mainphp = 'views/scripts/admin/index/dophoto.phtml';
		echo $this->view->render("index.phtml");
	}
	
	public function submitphoAction(){
		
		if($this->_request->isPost()){
			$filter = new Zend_Filter_StripTags();		
			
		
			$action = $this->_request->getPost('action');
			if ($action == 'edit') {	//修改
				$data = array(
					'pho_title'=>$this->_request->getPost('pho_title'),
				);
				$this->photo->update_pho($this->_request->getPost('pho_id'),$data);
				php_alert('修改照片成功',$this->view->adminurl."/phoclass");
				exit;
			}else if($action == 'add') {//新增
				//获取相片name数组
				$fileform = array();
				$photitles = array();
				for($i = 1; $i <= 5; $i++){
					if(isset($_FILES['pho_dir'.$i]["tmp_name"]) && trim($_FILES['pho_dir'.$i]["tmp_name"]) != ''):
					array_push($fileform,'pho_dir'.$i);
					array_push($photitles,$filter->filter($this->_request->getPost('pho_title'.$i)));
					else:
					continue;
					endif;
				}
				load_inc("upload.class");
				$upload = new upload_file(BLOG_ROOT_DIR."/upload",$fileform);
				$upload->thumbwidth = 120;
				$upload->thumbheight = 120;
				if(!$upload->move_upload()){
					php_alert($upload->error,-1);
					exit;
				}
				$phodirs = $upload->filearray;
				for($i = 0; $i < count($phodirs); $i ++){
					$data = array(
						'pho_title'=>$photitles[$i],
						'pho_lit_img'=>$phodirs[$i][1],
						'pho_big_img'=>$phodirs[$i][0],
						'pho_class_id'=>$this->_request->getPost('pho_class_id'),
						'pho_date'=>time(),
					);
					$this->photo->add_pho($data);
					if($i == count($phodirs)-1){//把最后一张照片缩略图默认写进该类别推荐
						load_model('Class');
						$this->class = new APclass();
						$data = array(
							'class_recom_pho' => $phodirs[$i][1],
						);
						$this->class->update_class($this->_request->getPost('pho_class_id'),$data);
					}
				}
				php_alert("添加照片成功",$this->view->adminurl."/phoclass");
				exit;
			}
			//$this->_redirect("admin/phoclass");
		}else if(isset($_GET["action"]) && $_GET["action"] == 'del'){
			if(isset($_GET["id"])):
			$this->photo->del_pho($_GET["id"]);
			php_alert("删除照片成功",$this->view->adminurl."/phoclass");
			else:
			php_alert("参数不完整",$this->view->adminurl."/phoclass");
			endif;
			exit;
		}
	}
	
	public function noRouteAction(){
		$this->_redirect('/');
	}
	
}
?>